package com.skywolf.chem.match.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skywolf.chem.utils.HttpUtils;
import com.skywolf.chem.utils.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by pei_pei on 2021/10/8.
 */
public class LocalAddress {
    private static final Logger logger = LoggerFactory.getLogger(LocalAddress.class);

    public static Map<String,String> getLocal(String address) {
        String gaodeKey = SysConfig._this.getProperty("gaode.key");
        //1、通过java api从高德地图获取经纬度
        Map<String, String> result = new HashMap<>();
        HashMap paramsMap = new HashMap<>();
        try {
            paramsMap.put("address", address);
            paramsMap.put("output", "JSON");
            paramsMap.put("key", gaodeKey);
            String response = HttpUtils.getMap("https://restapi.amap.com/v3/geocode/geo", paramsMap);
            JSONObject jsonNode  = JSONObject.parseObject(response);
            if(jsonNode.getString("status").equals("1")) {
                JSONObject objLocal = jsonNode.getJSONArray("geocodes").getJSONObject(0);
                String[] local = objLocal.getString("location").split(",");
                result.put("longitude",local[0]);
                result.put("latitude",local[1]);
                return result;
            }
            return null;
        } catch (Exception e) {
            logger.error("LocalAddress error data:" + JSON.toJSONString(paramsMap) + ",error:{}", e);
        }
        return null;

    }

    /**
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getAddresses(String ip) {
        // 这里调用淘宝API
        String urlStr = "https://restapi.amap.com/v3/ip?parameters";
        Map<String,String> map =new HashMap<>();
        String gaodeKey = "62bf9400bfc229ea7b13a46e8a449490";//SysConfig._this.getProperty("gaode.key");
        map.put("key",gaodeKey);
        map.put("ip",ip);
        map.put("type","4");
        String returnStr = HttpUtils.getMap(urlStr,map);
        if (returnStr != null) {
            return  JSON.parseObject(returnStr).getString("city");
        }
        return null;
    }

    /**
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getCityName() {
        // 这里调用淘宝API
        String urlStr = "https://restapi.amap.com/v3/geocode/regeo";
        Map<String,String> map =new HashMap<>();
        String gaodeKey = "62bf9400bfc229ea7b13a46e8a449490";//SysConfig._this.getProperty("gaode.key");
        map.put("key",gaodeKey);
        map.put("location","104.10194,30.65984");
        map.put("poitype","radius");
        map.put("extensions","base");
        map.put("batch","true");
        String returnStr = HttpUtils.getMap(urlStr,map);
        if (returnStr != null) {
            JSONArray arr = JSON.parseObject(returnStr).getJSONArray("regeocodes");
            if(arr!=null){
                JSONObject obj = arr.getJSONObject(0).getJSONObject("addressComponent");
                if(obj!=null){
                    return obj.getString("province")+"-"+obj.getString("city");
                }
            }
        }
        return null;
    }

    public static void main(String[] arg){
        System.out.println(getCityName());
    }
}
